package ge.telasi.tasks.ui.appconfig;

import ge.telasi.tasks.model.AppConfig;
import ge.telasi.tasks.model.copy.Copier;
import ge.telasi.tasks.ui.comp.TableModel;
import ge.telasi.tasks.ui.Platform;
import ge.telasi.tasks.ui.comp.SimpleTable;
import ge.telasi.tasks.ui.comp.TableUtils;
import ge.telasi.tasks.ui.log.Loggable;
import ge.telasi.tasks.ui.log.LoggerUtils;
import java.util.List;

/**
 * @author dimitri
 */
public class AppConfigTable extends SimpleTable<AppConfig> implements Loggable {
    static final int KEY = 0;
    static final int VALUE = 1;

    @Override
    protected TableModel<AppConfig> createEmptyModel() {
        return new AppConfigTableModel();
    }

    @Override
    protected List<AppConfig> getDataFromServer( Platform pl) throws Exception {
        return pl.getFacade().getAppConfigs(pl.getCredentials());
    }

    @Override
    protected String getPreferenceName() {
        return "SimpleTable.AppConfigTable";
    }

    @Override
    protected int[] getDefaultColumnWidthes() {
        return new int[] {100, 100};
    }

    class AppConfigTableModel extends TableModel<AppConfig> {

        @Override
        public int getColumnCount() {
            return 2;
        }

        @Override
        public String getColumnName(int column) {
            switch (column) {
                case KEY:
                    return "კოდური სახელი";
                case VALUE:
                    return "მნიშვნელობა";
                default:
                    return "?";
            }
        }

        @Override
        public Class<?> getColumnClass(int column) {
            switch (column) {
                case KEY:
                case VALUE:
                    return String.class;
                default:
                    return Object.class;
            }
        }

        @Override
        public boolean isCellEditable(int row, int column) {
            switch (column) {
                case VALUE:
                    return true;
                default:
                    return false;
            }
        }

        @Override
        public Object getValueAt(int row, int column) {
            AppConfig config = getObjectAt(row);
            switch (column) {
                case KEY:
                    return config.getKey();
                case VALUE:
                    return config.getValue();
                default:
                    return "?";
            }
        }

        @Override
        public void setValueAt(Object aValue, int row, int column) {
            AppConfig config = getObjectAt(row);
            switch(column) {
                case VALUE:
                    if (Platform.getDefault().validateConnection()) {
                        try {
                            AppConfig newConfig = Copier.copy(config);
                            newConfig.setValue((String) aValue);
                            Platform pl = Platform.getDefault();
                            newConfig = pl.getFacade().updateAppConfig(pl.getCredentials(), newConfig);
                            replaceItemAt(newConfig, row);
                            TableUtils.setSelectionAt(AppConfigTable.this, row);
                        } catch (Exception ex) {
                            LoggerUtils.manageException(AppConfigTable.this, getLogger(), ex);
                        }
                    }
                    break;
                default:
                    break;
            }
        }

    }

}
